package com.joolun.web.vo;

import com.joolun.script.entity.Script;
import com.joolun.weixin.utils.GsonUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.List;

@Data
@ApiModel(description = "剧本")
public class ScriptVO {

    /**
     *
     */
    @NotNull(message = "不能为空")
    @ApiModelProperty(value = "")
    private String id;
    /**
     * 封面
     */
    @ApiModelProperty(value = "封面")
    private String cover;
    /**
     * 标题
     */
    @ApiModelProperty(value = "标题")
    private String title;
    /**
     * 题材类型
     */
    @ApiModelProperty(value = "题材类型")
    private String themeType;
    /**
     * 剧本介绍
     */
    @ApiModelProperty(value = "剧本介绍")
    private String remark;

    /**
     * 角色介绍
     */
    @ApiModelProperty(value = "角色介绍")
    private List<ScriptRole> roleList;
    /**
     * 作者寄语
     */
    @ApiModelProperty(value = "作者寄语")
    private String authorNote;
    /**
     * 创建时间
     */
    @NotNull(message = "创建时间不能为空")
    @ApiModelProperty(value = "创建时间")
    private LocalDateTime createTime;
    /**
     * 推荐：Y是，N否
     */
    @NotNull(message = "推荐：Y是，N否不能为空")
    @ApiModelProperty(value = "推荐：Y是，N否")
    private String recommend;

    public static ScriptVO toScriptVO(Script script) {
        ScriptVO vo = new ScriptVO();
        BeanUtils.copyProperties(script, vo);
        String roles = script.getRoles();
        if (StringUtils.isNotBlank(roles)) {
            List<ScriptRole> scriptRoleList = GsonUtil.gsonToList(roles, ScriptRole.class);
            vo.setRoleList(scriptRoleList);
        }
        return vo;
    }

    public Script toScript() {
        Script script = new Script();
        BeanUtils.copyProperties(this, script);
        List<ScriptRole> roleList = this.getRoleList();
        if (CollectionUtils.isEmpty(roleList)) {
            script.setRoles("");
        } else {
            script.setRoles(GsonUtil.toGsonString(roleList));
        }
        return script;
    }

}
